package com.exp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exp.dto.JobPageQueryDTO;
import com.exp.mapper.EmployeeInfMapper;
import com.exp.mapper.JobInfMapper;
import com.exp.pojo.DeptInf;
import com.exp.pojo.JobInf;
import com.exp.pojo.UserInf;
import com.exp.result.PageResult;
import com.exp.service.JobInfService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author 王-自信
* @description 针对表【job_inf】的数据库操作Service实现
* @createDate 2024-11-18 20:52:04
*/
@Service
public class JobInfServiceImpl extends ServiceImpl<JobInfMapper, JobInf>
    implements JobInfService{

    @Autowired
    private JobInfMapper jobInfMapper;
    @Autowired
    private EmployeeInfMapper employeeInfMapper;
    @Override
    public PageResult pageQuery(JobPageQueryDTO jobPageQueryDTO) {
        // select * from employee limit 0,10
        //开始分页查询
        PageHelper.startPage(jobPageQueryDTO.getPage(), jobPageQueryDTO.getPageSize());

        Page<JobInf> page = jobInfMapper.pageQuery(jobPageQueryDTO);

        long total = page.getTotal();
        List<JobInf> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    public void update(JobInf jobInf) {
        jobInfMapper.update(jobInf);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        //判断是否能够删除---是否被员工相关业务关联
        List<Integer> employeeIds = jobInfMapper.getEmployeeIdsByJobIds(ids);
        System.out.println("关联员工相关业务id集合：" + employeeIds);
        Set<String> docSet = new HashSet<>(); // 使用HashSet来存储不重复的用户名

        if (employeeIds != null && !employeeIds.isEmpty()) {
            System.out.println("----员工相关业务----");
            for (Integer id : employeeIds) {
                Integer employeeId = employeeInfMapper.selectById(id).getJobId();
                JobInf jobInf = jobInfMapper.selectById(employeeId);
                docSet.add(jobInf.getName()); // 将用户名添加到Set集合中
            }

            if (!docSet.isEmpty()) {
                // 将Set集合转换为字符串，用逗号分隔
                String doc = String.join(",", docSet);
                // 被关联了，不能删除
                throw new RuntimeException("职位 " + doc + " 被员工相关业务关联了，不能删除");
            }
        }

        //删除数据
        for (Integer id : ids) {
            jobInfMapper.deleteById(id);
        }
    }
}




